package jp.sourceforge.akjrcp.youtube;

import jp.sourceforge.akjrcp.akj_betas.Akj_betasPlugin;
import jp.sourceforge.akjrcp.akj_betas.Messages;
import jp.sourceforge.akjrcp.youtubedl.YoutubeDLView;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.browser.LocationEvent;
import org.eclipse.swt.browser.LocationListener;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;


public class YoutubeViewer extends ViewPart {
	public static Log log=LogFactory.getLog(YoutubeViewer.class);
	
	public static final String ID = YoutubeViewer.class.getName();

	private Browser browser;

	private StackLayout stack;

	private Composite browserComposite;
	/**
	 * This is a callback that will allow us to create the viewer and initialize
	 * it.
	 */
	
	public void openURL(String url){
		if(stack.topControl!=browserComposite){
		stack.topControl=browserComposite;
		browserComposite.getParent().layout();
		}
		browser.setUrl(url);
	}
	public void createPartControl(Composite parent) {
		stack = new StackLayout();
		parent.setLayout(stack);
		
		Button bt=new Button(parent,SWT.NULL);
		bt.setText("http://jp.youtube.com");
		bt.addSelectionListener(new SelectionListener(){

			public void widgetDefaultSelected(SelectionEvent e) {
				// TODO Auto-generated method stub
				
			}

			public void widgetSelected(SelectionEvent e) {
				
				openURL("http://jp.youtube.com");
			}
			
		});
		
		
		browserComposite=new Composite(parent,SWT.NULL);
		browserComposite.setLayout(new GridLayout(1,false));
		
		Composite panel=new Composite(browserComposite,SWT.NULL);
		panel.setLayout(new GridLayout(2,false));
		panel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		Button back=new Button(panel,SWT.NULL);
		back.setText(Messages.getString("youtubeview.back"));
		back.addSelectionListener(new SelectionListener(){

			public void widgetDefaultSelected(SelectionEvent e) {
			}

			public void widgetSelected(SelectionEvent e) {
				browser.back();
			}
			
		});
		
		Button forward=new Button(panel,SWT.NULL);
		forward.setText(Messages.getString("youtubeview.foward"));
		forward.addSelectionListener(new SelectionListener(){

			public void widgetDefaultSelected(SelectionEvent e) {
			}

			public void widgetSelected(SelectionEvent e) {
				browser.forward();
			}
			
		});
		
		//parent.setLayout(new FillLayout());
		browser = new Browser(browserComposite,SWT.NULL);
		browser.setLayoutData(new GridData(GridData.FILL_BOTH));
		//browser.setUrl("http://www.youtube.com");
		browser.addLocationListener(new LocationListener(){

			public void changed(LocationEvent event) {
				/*
				String url=event.location;
				
				//if(YoutubeUtils.isYoutubeVideoURL(url)){
					YoutubeDLView view=(YoutubeDLView)PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView(YoutubeDLView.ID);
					if(view!=null){
						view.setUrl(url);
					}
					*/
				//}
				/*
				ImageCapture capture=new ImageCapture();
				
				
				log.infochanged:"+event.location);
				GC gc = new GC(browser);
				final Image image =
					new Image(PlatformUI.getWorkbench().getDisplay(), browser.getSize().x,browser.getSize().y);
				gc.copyArea(image, 0, 0);
				gc.dispose();
				
				//Image image=capture.getImage(browser,true);
				ImageLoader loader=new ImageLoader();
				loader.data=new ImageData[]{image.getImageData()};
				loader.save("c:\\tmp\\t2.jpg", SWT.IMAGE_JPEG);
				*/
				//loader
				/*
				try {
					//only in screen
					Robot robot = new Robot();
					BufferedImage img=robot.createScreenCapture(new Rectangle(0,720,320,1000));
					try {
						ImageIO.write(img, "jpg", new File("c:\\tmp\\t.jpg"));
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				} catch (AWTException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}*/
				System.out.println("ch:"+event.location);
				
			}

			public void changing(LocationEvent event) {
				//changing much faster
				String url=event.location;
				
				//if(YoutubeUtils.isYoutubeVideoURL(url)){
					YoutubeDLView view=(YoutubeDLView)PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView(YoutubeDLView.ID);
					if(view!=null){
						view.setUrl(url);
					}
			}});
		
		stack.topControl=bt;
		
	}
	
	public void setFocus() {
		
	}
	
	

	
	public void debugArray(String[] array){
		String v="";
		for (int i = 0; i < array.length; i++) {
			v+="\""+array[i]+"\""+" ";
		}
		log.info(v);
		Akj_betasPlugin.getDefault().getProcessPrinter().printOutput(v+"\n");
	}
	
	
	
	
		
}